package com.yanqu.road.dao.impl.activity.recallplayer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.recallplayer.data.RecallPlayerBeBindTaskValue;
import com.yanqu.road.entity.activity.recallplayer.data.UserRecallOtherData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserRecallOtherDataDaoImpl extends TempDataObjectDao<UserRecallOtherData> {

    public static String getAllUserRecallOtherDataSql = "select * from `t_u_recall_other_data`";

    public Map<Long, Map<Integer, List<UserRecallOtherData>>> getAllUserRecallOtherData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, List<UserRecallOtherData>>> allUserRecallOtherMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAllUserRecallOtherDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserRecallOtherData recallOtherData = parse(rs);
                long userId = recallOtherData.getUserId();
                int playerType = recallOtherData.getPlayerType();
                if (allUserRecallOtherMap.get(userId) == null) {
                    allUserRecallOtherMap.put(userId, new ConcurrentHashMap<>());
                }
                if (allUserRecallOtherMap.get(userId).get(playerType) == null) {
                    allUserRecallOtherMap.get(userId).put(playerType, new ArrayList<>());
                }
                allUserRecallOtherMap.get(userId).get(playerType).add(recallOtherData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return allUserRecallOtherMap;
    }


    public static String getUserRecallOtherDataSql = "select * from `t_u_recall_other_data` where `userId`=?";

    public Map<Integer, List<UserRecallOtherData>> getUserRecallOtherData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UserRecallOtherData>> userRecallOtherMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserRecallOtherDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserRecallOtherData recallOtherData = parse(rs);
                int playerType = recallOtherData.getPlayerType();
                if (userRecallOtherMap.get(playerType) == null) {
                    userRecallOtherMap.put(playerType, new ArrayList<>());
                }
                userRecallOtherMap.get(playerType).add(recallOtherData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userRecallOtherMap;
    }

    /**
     * 读取baseInfo
     */
    public static String loadUserBaseInfoDataSql = "SELECT userBaseInfo,recallUserId FROM `t_u_recall_other_data` WHERE userId=?";

    public Map<Long, UserBaseInfo> loadUserBaseInfo(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserBaseInfo> userBaseInfoMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(loadUserBaseInfoDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBaseInfo userBaseInfo = parseUserInfo(rs);
                long recallUserId = rs.getLong("recallUserId");
                userBaseInfoMap.put(recallUserId, userBaseInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userBaseInfoMap;


    }

    /**
     * 更新baseinfo
     */
    public static String updateUserInfoSql = "update `t_u_recall_other_data` set userBaseInfo=? where userId=? and recallUserId=?";

    public boolean updateUserInfoData(UserBaseInfo userBaseInfo, long userId, long recallUserId) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userBaseInfo)));
        para.put(i++, new DbParameter(Types.BIGINT, userId));
        para.put(i++, new DbParameter(Types.BIGINT, recallUserId));
        if (getDb().executeNoneQuery(updateUserInfoSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserRecallOtherDataSql = "update `t_u_recall_other_data` set recallUserServer=?,playerType=?,recallTime=?,taskData=?,hadInit=? where userId=? and recallUserId=?";

    @Override
    protected boolean updateData(UserRecallOtherData userRecallOtherData) {
        userRecallOtherData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallUserServer()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallOtherData.getPlayerType()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallOtherData.getTaskData())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userRecallOtherData.isHadInit()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallUserId()));
        if (getDb().executeNoneQuery(updateUserRecallOtherDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserRecallOtherDataSql = "insert into `t_u_recall_other_data`(userId,recallUserId,recallUserServer,playerType,recallTime,taskData,userBaseInfo,hadInit) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserRecallOtherData userRecallOtherData) {
        userRecallOtherData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallUserServer()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallOtherData.getPlayerType()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallOtherData.getRecallTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallOtherData.getTaskData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallOtherData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userRecallOtherData.isHadInit()));
        if (getDb().executeNoneQuery(insertUserRecallOtherDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    protected UserBaseInfo parseUserInfo(ResultSet rs) throws SQLException {
        return JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class);
    }

    @Override
    protected UserRecallOtherData parseData(ResultSet rs) throws SQLException {
        UserRecallOtherData userRecallOtherData = new UserRecallOtherData();
        userRecallOtherData.setUserId(rs.getLong("userId"));
        userRecallOtherData.setRecallUserId(rs.getLong("recallUserId"));
        userRecallOtherData.setRecallUserServer(rs.getLong("recallUserServer"));
        userRecallOtherData.setPlayerType(rs.getInt("playerType"));
        userRecallOtherData.setRecallTime(rs.getLong("recallTime"));
        userRecallOtherData.setTaskData(JSON.parseObject(rs.getString("taskData"), new TypeReference<List<RecallPlayerBeBindTaskValue>>() {
        }));
        //userRecallOtherData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        userRecallOtherData.setHadInit(rs.getBoolean("hadInit"));

        return userRecallOtherData;
    }

}
